from rest_framework.views import APIView
from rest_framework.response import Response
import json

from course import models, courseser


# Create your views here.

class HomeCate(APIView):
    def get(self, request):
        allcate = models.Category.objects.filter(parent=None, flag=True).order_by('order')
        ser = courseser.CategorySerializer(allcate, many=True)
        data = ser.data
        for item in data:
            item['children'] = item['children'][:3]
        return Response({'code': 200, 'msg': 'ok', 'data': ser.data})


class HomeChile(APIView):
    def get(self, request):
        cate = request.query_params.get('cate', '')
        if not cate:
            return Response("参数不对")
        catelist = models.Category.objects.get(id=cate, flag=True)
        ser = courseser.CategorySerializer(catelist)
        data = ser.data
        data['course'] = []
        return Response({'code': 200, 'msg': 'ok', 'data': data})
